import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _forEach from "lodash/forEach";
import { createPagination, getResponse } from 'utils/utils';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { uiPageOrgCreate, uiPageOrgQueryPaging, uiPageOrgUpdate, // detail
uiPageOrgQueryDetail, uiPageOrgDetailUpdate } from "../services/uiPageOrgService";
export default {
  namespace: 'uiPageOrg',
  state: {
    // 列表
    list: {},
    pagination: false,
    // 详情
    config: {},
    // 值集
    detailIdp: {}
  },
  effects: {
    fetchPageList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchPageList(_ref, _ref2) {
      var payload, put, call, organizationId, params, list;
      return _regeneratorRuntime.wrap(function fetchPageList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              put = _ref2.put, call = _ref2.call;
              organizationId = payload.organizationId, params = payload.params;
              _context.t0 = getResponse;
              _context.next = 6;
              return call(uiPageOrgQueryPaging, organizationId, params);

            case 6:
              _context.t1 = _context.sent;
              list = (0, _context.t0)(_context.t1);

              if (!list) {
                _context.next = 11;
                break;
              }

              _context.next = 11;
              return put({
                type: 'updateState',
                payload: {
                  list: list,
                  pagination: createPagination(list)
                }
              });

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, fetchPageList);
    }),
    listCreateOne: /*#__PURE__*/_regeneratorRuntime.mark(function listCreateOne(_ref3, _ref4) {
      var payload, call, organizationId, data;
      return _regeneratorRuntime.wrap(function listCreateOne$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              organizationId = payload.organizationId, data = payload.data;
              _context2.t0 = getResponse;
              _context2.next = 6;
              return call(uiPageOrgCreate, organizationId, data);

            case 6:
              _context2.t1 = _context2.sent;
              return _context2.abrupt("return", (0, _context2.t0)(_context2.t1));

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, listCreateOne);
    }),
    listUpdateOne: /*#__PURE__*/_regeneratorRuntime.mark(function listUpdateOne(_ref5, _ref6) {
      var payload, call, organizationId, data;
      return _regeneratorRuntime.wrap(function listUpdateOne$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              organizationId = payload.organizationId, data = payload.data;
              _context3.t0 = getResponse;
              _context3.next = 6;
              return call(uiPageOrgUpdate, organizationId, data);

            case 6:
              _context3.t1 = _context3.sent;
              return _context3.abrupt("return", (0, _context3.t0)(_context3.t1));

            case 8:
            case "end":
              return _context3.stop();
          }
        }
      }, listUpdateOne);
    }),
    fetchDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDetail(_ref7, _ref8) {
      var payload, put, call, organizationId, pageCode, res, config, idpRes, componentType, componentTypeMeaning;
      return _regeneratorRuntime.wrap(function fetchDetail$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              put = _ref8.put, call = _ref8.call;
              organizationId = payload.organizationId, pageCode = payload.pageCode;
              _context4.next = 5;
              return call(uiPageOrgQueryDetail, organizationId, pageCode);

            case 5:
              res = _context4.sent;
              config = getResponse(res);
              _context4.next = 9;
              return call(queryIdpValue, 'CPFM.UI.COMPONENT');

            case 9:
              idpRes = _context4.sent;
              componentType = getResponse(idpRes); // if (config) {
              //   const orgConfig = {...config};
              //   const tpls = orgConfig.fields || [];
              //   const newTpls = [];
              //   for (let tplIndex = 0; tplIndex < tpls.length; tplIndex += 1) {
              //     const tpl = tpls[tplIndex];
              //     const newTpl = {...tpl};
              //     const removeSiteFields = [];
              //     const noRemoveFields = [];
              //     newTpl.removeSiteFields = removeSiteFields;
              //     const fields = tpl.fields || [];
              //     switch (tpl.templateType) {
              //       case 'DynamicForm':
              //       case 'DynamicTable':
              //       case 'DynamicToolbar':
              //         for (let fieldIndex = 0; fieldIndex <
              //         fields.length; fieldIndex += 1) {
              //           const field = fields[fieldIndex];
              //           if (field.visiableFlag === 1) {
              //             noRemoveFields.push(field);
              //           } else if (field.siteFlag === 1) {
              //             removeSiteFields.push(field);
              //           }
              //         }
              //         newTpl.removeSiteFields = removeSiteFields;
              //         newTpl.fields = noRemoveFields;
              //         break;
              //       default:
              //         break;
              //     }
              //     newTpls.push(newTpl);
              //   }
              //   orgConfig.fields = newTpls;
              //   yield put({
              //     type: 'updateState',
              //     payload: {
              //       config: orgConfig,
              //     },
              //   });
              // }

              if (!config) {
                _context4.next = 14;
                break;
              }

              _context4.next = 14;
              return put({
                type: 'updateState',
                payload: {
                  config: config
                }
              });

            case 14:
              if (!componentType) {
                _context4.next = 19;
                break;
              }

              componentTypeMeaning = {};

              _forEach(componentType, function (idp) {
                componentTypeMeaning[idp.value] = idp.meaning;
              });

              _context4.next = 19;
              return put({
                type: 'updateDetailIdp',
                payload: {
                  componentType: componentType,
                  componentTypeMeaning: componentTypeMeaning
                }
              });

            case 19:
              return _context4.abrupt("return", config);

            case 20:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchDetail);
    }),
    detailUpdate: /*#__PURE__*/_regeneratorRuntime.mark(function detailUpdate(_ref9, _ref10) {
      var payload, call, organizationId, config, res;
      return _regeneratorRuntime.wrap(function detailUpdate$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              organizationId = payload.organizationId, config = payload.config;
              _context5.t0 = getResponse;
              _context5.next = 6;
              return call(uiPageOrgDetailUpdate, organizationId, config);

            case 6:
              _context5.t1 = _context5.sent;
              res = (0, _context5.t0)(_context5.t1);
              return _context5.abrupt("return", res);

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, detailUpdate);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref11) {
      var payload = _ref11.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    updateDetailIdp: function updateDetailIdp(state, _ref12) {
      var payload = _ref12.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        detailIdp: _objectSpread(_objectSpread({}, state.detailIdp), payload)
      });
    }
  }
};